<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="bill_num" prop="billNum">
        <el-input
          v-model="queryParams.billNum"
          placeholder="请输入账单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业id" prop="enterpriseId">
        <el-input
          v-model="queryParams.enterpriseId"
          placeholder="请输入企业id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抄表记录id" prop="checkMeterId">
        <el-input
          v-model="queryParams.checkMeterId"
          placeholder="请输入抄表记录id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仪表Id" prop="meterId">
        <el-input
          v-model="queryParams.meterId"
          placeholder="请输入仪表Id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账单起始日期" prop="beginDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.beginDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择账单起始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="账单结束日期" prop="endDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择账单结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实用金额" prop="feeAmount">
        <el-input
          v-model="queryParams.feeAmount"
          placeholder="请输入实用金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分摊系数" prop="shareCoefficient">
        <el-input
          v-model="queryParams.shareCoefficient"
          placeholder="请输入分摊系数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分摊量" prop="shareQuantity">
        <el-input
          v-model="queryParams.shareQuantity"
          placeholder="请输入分摊量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总金额" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入总金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分摊金额" prop="shareAmount">
        <el-input
          v-model="queryParams.shareAmount"
          placeholder="请输入分摊金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="园区id" prop="parkId">
        <el-input
          v-model="queryParams.parkId"
          placeholder="请输入园区id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标准类型(水表0,1电表)" prop="meterType">
        <el-select v-model="queryParams.meterType" placeholder="请选择标准类型(水表0,1电表)" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="费用周期" prop="feeCycle">
        <el-date-picker clearable size="small"
          v-model="queryParams.feeCycle"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择费用周期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="固废" prop="fixedFee">
        <el-input
          v-model="queryParams.fixedFee"
          placeholder="请输入固废"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input
          v-model="queryParams.unitPrice"
          placeholder="请输入单价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['life:feMeterBill:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['life:feMeterBill:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['life:feMeterBill:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['life:feMeterBill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feMeterBillList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账单编号" align="center" prop="billNum" min-width="100"/>
      <el-table-column label="仪表类型" align="center" prop="meterType" >
        <template slot-scope="scope">
          <span>{{ scope.row.meterType==0?"水表":"电表" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="费用周期" align="center" prop="feeCycle" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.feeCycle, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上月底数"
                       align="center"
                       prop="lastValue" />

      <el-table-column label="本月止数"
                       align="center"
                       prop="currentValue" />

      <el-table-column label="单价"
                       align="center"
                       prop="unitPrice">
      </el-table-column>
      <el-table-column label="读数用量"
                       align="center"
                       prop="actualDosage">
      </el-table-column>
      <el-table-column label="读数金额"
                       align="center"
                       prop="feeAmount">
      </el-table-column>
      <el-table-column label="分摊系数" align="center" prop="shareCoefficient" />
      <el-table-column label="分摊量" align="center" prop="shareQuantity" />
      <el-table-column label="分摊金额" align="center" prop="shareAmount" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
<!--      <el-table-column label="园区id" align="center" prop="parkId" />-->
<!--      <el-table-column label="固废" align="center" prop="fixedFee" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--&lt;!&ndash;          <el-button&ndash;&gt;-->
<!--&lt;!&ndash;            size="mini"&ndash;&gt;-->
<!--&lt;!&ndash;            type="text"&ndash;&gt;-->
<!--&lt;!&ndash;            icon="el-icon-edit"&ndash;&gt;-->
<!--&lt;!&ndash;            @click="handleUpdate(scope.row)"&ndash;&gt;-->
<!--&lt;!&ndash;            v-hasPermi="['life:feMeterBill:edit']"&ndash;&gt;-->
<!--&lt;!&ndash;          >修改</el-button>&ndash;&gt;-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['life:feMeterBill:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改水电明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="bill_num" prop="billNum">
          <el-input v-model="form.billNum" placeholder="请输入bill_num" />
        </el-form-item>
        <el-form-item label="企业id" prop="enterpriseId">
          <el-input v-model="form.enterpriseId" placeholder="请输入企业id" />
        </el-form-item>
        <el-form-item label="抄表记录id" prop="checkMeterId">
          <el-input v-model="form.checkMeterId" placeholder="请输入抄表记录id" />
        </el-form-item>
        <el-form-item label="仪表Id" prop="meterId">
          <el-input v-model="form.meterId" placeholder="请输入仪表Id" />
        </el-form-item>
        <el-form-item label="账单起始日期" prop="beginDate">
          <el-date-picker clearable size="small"
            v-model="form.beginDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择账单起始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="账单结束日期" prop="endDate">
          <el-date-picker clearable size="small"
            v-model="form.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择账单结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实用金额" prop="feeAmount">
          <el-input v-model="form.feeAmount" placeholder="请输入实用金额" />
        </el-form-item>
        <el-form-item label="分摊系数" prop="shareCoefficient">
          <el-input v-model="form.shareCoefficient" placeholder="请输入分摊系数" />
        </el-form-item>
        <el-form-item label="分摊量" prop="shareQuantity">
          <el-input v-model="form.shareQuantity" placeholder="请输入分摊量" />
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="分摊金额" prop="shareAmount">
          <el-input v-model="form.shareAmount" placeholder="请输入分摊金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="园区id" prop="parkId">
          <el-input v-model="form.parkId" placeholder="请输入园区id" />
        </el-form-item>
        <el-form-item label="标准类型(水表0,1电表)" prop="meterType">
          <el-select v-model="form.meterType" placeholder="请选择标准类型(水表0,1电表)">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="费用周期" prop="feeCycle">
          <el-date-picker clearable size="small"
            v-model="form.feeCycle"
            type="date"
            value-format="yyyy-MM"
            placeholder="选择费用周期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="固废" prop="fixedFee">
          <el-input v-model="form.fixedFee" placeholder="请输入固废" />
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入单价" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeMeterBill, getFeMeterBill, delFeMeterBill, addFeMeterBill, updateFeMeterBill } from "@/api/life/feMeterBill";

export default {
  name: "FeMeterBill",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水电明细表格数据
      feMeterBillList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billNum: null,
        enterpriseId: null,
        checkMeterId: null,
        meterId: null,
        beginDate: null,
        endDate: null,
        feeAmount: null,
        shareCoefficient: null,
        shareQuantity: null,
        totalAmount: null,
        shareAmount: null,
        parkId: null,
        meterType: null,
        feeCycle: null,
        fixedFee: null,
        unitPrice: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询水电明细列表 */
    getList() {
      this.loading = true;
      listFeMeterBill(this.queryParams).then(response => {
        this.feMeterBillList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billNum: null,
        enterpriseId: null,
        checkMeterId: null,
        meterId: null,
        beginDate: null,
        endDate: null,
        feeAmount: null,
        shareCoefficient: null,
        shareQuantity: null,
        totalAmount: null,
        shareAmount: null,
        createTime: null,
        updateTime: null,
        remark: null,
        parkId: null,
        meterType: null,
        feeCycle: null,
        fixedFee: null,
        unitPrice: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加水电明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFeMeterBill(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改水电明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFeMeterBill(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFeMeterBill(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除水电明细编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFeMeterBill(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('life/feMeterBill/export', {
        ...this.queryParams
      }, `life_feMeterBill.xlsx`)
    }
  }
};
</script>
